package com.java.controller;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.java.entity.BsAdmin;
import com.java.entity.BsAppraise;
import com.java.entity.BsAppraiseRecord;
import com.java.entity.BsCourse;
import com.java.entity.BsCourseTarget;
import com.java.entity.BsPaper;
import com.java.entity.BsPaperItem;
import com.java.entity.BsScore;
import com.java.entity.BsScoreRecord;
import com.java.entity.BsStudent;
import com.java.entity.dtoVo.BsStudentDto;
import com.java.service.BsAdminService;
import com.java.service.BsAppraiseService;
import com.java.service.BsCourseService;
import com.java.service.BsPaperService;
import com.java.service.BsStudentService;
import com.java.util.MyCons;
import com.java.util.StudentListener;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@RestController
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private BsStudentService studentService;
	@Autowired
	private BsCourseService courseService;
	@Autowired
	private BsAppraiseService appraiseService;
	@Autowired
	private BsPaperService paperService;
	@Autowired
	private BsAdminService adminService;

	@PostMapping("/info")
	public BaseRespone info() {
		BsAdmin user = (BsAdmin) ServletUtil.getSession().getAttribute(MyCons.ADMIN_ROLE);
		BsAdmin admin = adminService.getAdmin(user.getAdminId());
		return BaseRespone.success(admin);
	}

	@PostMapping("/saveStudent")
	public BaseRespone saveStudent(@RequestBody BsStudent student) {
		return studentService.updateByAdmin(student);
	}

	@PostMapping("/saveInfo")
	public BaseRespone saveInfo(@RequestBody BsAdmin admin) {
		return adminService.update(admin);
	}

	@PostMapping("/getStudents")
	public TableRespone getStudents(@RequestParam(required = false) String number,
			@RequestParam(required = false) String name, @RequestParam(required = false) Integer active,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return studentService.getStudentList(number, name, active, page, size);
	}

	@PostMapping("/studentActive")
	public BaseRespone studentActive(@RequestParam(required = false) String id,
			@RequestParam(required = false) Integer active) {
		return studentService.studentActive(id, active);
	}

	/**
	 * 课程与目标操作
	 */

	@PostMapping("/getCourses")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
	public TableRespone getCourses(@RequestParam(required = false) String title,
			@RequestParam(required = false) String typeId, @RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return courseService.getCourses(title, typeId, page, size);
	}

	@PostMapping("/saveCourse")
	public BaseRespone saveCourse(@RequestBody BsCourse course) {
		return courseService.saveCourse(course);
	}

	@PostMapping("/delCourse")
	public BaseRespone delCourse(String courseId) {
		return courseService.delCourse(courseId);
	}

	@PostMapping("/getTargets")
	public TableRespone getTargets(String courseId) {
		return courseService.getTargets(courseId);
	}

	@PostMapping("/getTarget")
	public BsCourseTarget getTarget(String targetId) {
		return courseService.getTarget(targetId);
	}

	@PostMapping("/saveTarget")
	public BaseRespone saveTarget(@RequestBody BsCourseTarget target) {
		return courseService.saveTarget(target);
	}

	@PostMapping("/delTarget")
	public BaseRespone delTarget(String targetId) {
		return courseService.delTarget(targetId);
	}

	/**
	 * 命题与题目操作
	 */

	@PostMapping("/getPapers")
	public TableRespone getPapers(@RequestParam(required = false) String title,
			@RequestParam(required = false) String typeId, @RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return paperService.getPapers(title, typeId, page, size);
	}

	@PostMapping("/savePaper")
	public BaseRespone savePaper(@RequestBody BsPaper paper) {
		return paperService.savePaper(paper);
	}

	@PostMapping("/delPaper")
	public BaseRespone delPaper(String paperId) {
		return paperService.delPaper(paperId);
	}

	@PostMapping("/getItems")
	public TableRespone getItems(String paperId) {
		return paperService.getItems(paperId);
	}

	@PostMapping("/saveItem")
	public BaseRespone saveItem(@RequestBody BsPaperItem item) {
		return paperService.saveItem(item);
	}

	@PostMapping("/delItem")
	public BaseRespone delItem(String itemId) {
		return paperService.delItem(itemId);
	}

	/**
	 * 成绩操作
	 */

	@PostMapping("/getScores")
	public TableRespone getScores(@RequestParam(required = false) String studentId,
			@RequestParam(required = false) String title, @RequestParam(required = false) String typeId,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return studentService.getScores(studentId, title, typeId, page, size);
	}

	@PostMapping("/getMoreScores")
	public TableRespone getMoreScores(@RequestParam(required = false) String scoreId,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return studentService.getScoresMore(scoreId, page, size);
	}

	@PostMapping("/getScoreGrouby")
	public List<Map<String, String>> getScoreGrouby() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		return list;
	}

	@PostMapping("/saveScore")
	public BaseRespone saveScore(@RequestBody BsScore score) {
		return studentService.saveScore(score);
	}

	@PostMapping("/reLoadScore")
	public BaseRespone reLoadScore(String scoreId) {
		return studentService.reLoadScore(scoreId);
	}

	@PostMapping("/delScore")
	public BaseRespone delScore(String scoreId) {
		return studentService.delScore(scoreId);
	}

	@PostMapping("/getScoreRecords")
	public TableRespone getScoreRecords(String scoreId) {
		return studentService.getRecords(scoreId);
	}

	@PostMapping("/saveScoreRecord")
	public BaseRespone saveScoreRecord(@RequestBody BsScoreRecord record) {
		return studentService.saveRecord(record);
	}

	@PostMapping("/delScoreRecord")
	public BaseRespone delScoreRecord(String recordId) {
		return studentService.delRecord(recordId);
	}

	/**
	 * 考核记录与目标分值操作
	 */

	@PostMapping("/getAppraises")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
	public TableRespone getAppraises(@RequestParam(required = false) String studentId,
			@RequestParam(required = false) String courseId, @RequestParam(required = false) String remark,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return appraiseService.getAppraises(studentId, courseId, remark, page, size);
	}

	@PostMapping("/saveAppraise")
	public BaseRespone saveAppraise(@RequestBody BsAppraise appraise) {
		return appraiseService.saveAppraise(appraise);
	}

	@PostMapping("/delAppraise")
	public BaseRespone delAppraise(String appraiseId) {
		return appraiseService.delAppraise(appraiseId);
	}

	@PostMapping("/reAppraise")
	public BaseRespone reAppraise(String appraiseId) {
		return appraiseService.reAppraise(appraiseId);
	}

	@PostMapping("/getRecords")
	public TableRespone getRecords(String appraiseId) {
		return appraiseService.getRecords(appraiseId);
	}

	@PostMapping("/saveRecord")
	public BaseRespone saveRecord(@RequestBody BsAppraiseRecord record) {
		return appraiseService.saveRecord(record);
	}

	@PostMapping("/delRecord")
	public BaseRespone delRecord(String recordId) {
		return appraiseService.delRecord(recordId);
	}

	@PostMapping("/getDegrees")
	public TableRespone getDegrees(@RequestParam(required = false) String studentId,
			@RequestParam(required = false) String courseId, @RequestParam(required = false) String targetId,
			@RequestParam(required = false) String remark, @RequestParam(required = false) String date,
			@RequestParam(required = false, defaultValue = "0") int page,
			@RequestParam(required = false, defaultValue = "0") int size) {
		return appraiseService.getDegrees(studentId, courseId, targetId, remark, date, page, size);
	}

	@PostMapping("/importExcel")
	public BaseRespone importExcel(MultipartFile file) {
		if (null == file) {
			return BaseRespone.error("请上传文件");
		}
		try {
			InputStream inputStream = file.getInputStream();
			List<BsStudentDto> lst = EasyExcel.read(inputStream)
					.registerReadListener(new StudentListener())
					.head(BsStudentDto.class)
					.sheet(0).headRowNumber(1).doReadSync();
			return studentService.saveBatch(lst);
		} catch (Exception exception) {
			return BaseRespone.error(exception.getMessage());
		}
	}
}
